home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / TakFlying.as < prev    next >
Text File  |  2007-09-27  |  9KB  |  302 lines

  1. class TakFlying extends Library.State
  2. {
  3.    static var UP_LEFT_LIMIT = 50;
  4.    static var UP_RIGHT_LIMIT = 550;
  5.    static var DOWN_LEFT_LIMIT = 50;
  6.    static var DOWN_RIGHT_LIMIT = 550;
  7.    static var START_SPEED = 60;
  8.    static var SPEED_LOOSE = 0.35;
  9.    static var GRAVITY_GAIN = 0.85;
  10.    static var MAX_DROP_SPEED = 20;
  11.    static var MAX_SPEED_AIR = 8;
  12.    static var ACCELERATION_AIR = 0.75;
  13.    static var DIRECTION_CHANGE_FACTOR_AIR = 0.85;
  14.    function TakFlying(__mcRef)
  15.    {
  16.       super(__mcRef);
  17.       this.bGoingLeft = false;
  18.       this.bGoingRight = false;
  19.       this.oKeysManager = new Library.Utils.KeysManager();
  20.       this.oKeysManager.setListenerForKey(this,38);
  21.       this.oKeysManager.setListenerForKey(this,40);
  22.       this.oKeysManager.setListenerForKey(this,37);
  23.       this.oKeysManager.setListenerForKey(this,39);
  24.       this.nFollowY = -50;
  25.       this.nSpeedX = 0;
  26.       this.nSpeedY = - TakFlying.START_SPEED;
  27.       this.setState("FlyUp");
  28.       MiniGameManager.__get__Instance().doAddListener(this);
  29.    }
  30.    static function get Instance()
  31.    {
  32.       return TakFlying.oCtrl;
  33.    }
  34.    function onKeyManagerEvent(__nEvent, __nCode)
  35.    {
  36.       if(!this.__get__Paused())
  37.       {
  38.          switch(__nEvent)
  39.          {
  40.             case Library.Utils.KeysManager.EVENT_KEY_DOWN:
  41.                switch(__nCode)
  42.                {
  43.                   case 37:
  44.                      this.bGoingLeft = true;
  45.                      break;
  46.                   case 39:
  47.                      this.bGoingRight = true;
  48.                      break;
  49.                   case 38:
  50.                      if(this.bFlyingDown)
  51.                      {
  52.                         this.setState("FlyDownSlow");
  53.                      }
  54.                      break;
  55.                   case 40:
  56.                      if(this.bFlyingDown)
  57.                      {
  58.                         this.setState("FlyDownFast");
  59.                      }
  60.                }
  61.                break;
  62.             case Library.Utils.KeysManager.EVENT_KEY_UP:
  63.                switch(__nCode)
  64.                {
  65.                   case 37:
  66.                      this.bGoingLeft = false;
  67.                      break;
  68.                   case 39:
  69.                      this.bGoingRight = false;
  70.                      break;
  71.                   case 38:
  72.                      if(this.bFlyingDown)
  73.                      {
  74.                         if(this.oKeysManager.isKeyDown(40))
  75.                         {
  76.                            this.setState("FlyDownFast");
  77.                         }
  78.                         else
  79.                         {
  80.                            this.setState("FlyDown");
  81.                         }
  82.                      }
  83.                      break;
  84.                   case 40:
  85.                      if(this.bFlyingDown)
  86.                      {
  87.                         if(this.oKeysManager.isKeyDown(38))
  88.                         {
  89.                            this.setState("FlyDownSlow");
  90.                         }
  91.                         else
  92.                         {
  93.                            this.setState("FlyDown");
  94.                         }
  95.                      }
  96.                }
  97.          }
  98.       }
  99.    }
  100.    function doSoundEvent(__nEvent, __oSound)
  101.    {
  102.       if(__nEvent === Library.Sound.SoundManager.EVENT_SOUND_COMPLETE)
  103.       {
  104.          if(__oSound == this.oFlappingSound)
  105.          {
  106.             delete this.oFlappingSound;
  107.          }
  108.       }
  109.    }
  110.    function doPause()
  111.    {
  112.       super.doPause();
  113.       this.oFlappingSound.doPause();
  114.    }
  115.    function doResume()
  116.    {
  117.       super.doResume();
  118.       this.oFlappingSound.doResume();
  119.    }
  120.    function doDestroy()
  121.    {
  122.       this.oKeysManager.doDestroy();
  123.       delete this.oKeysManager;
  124.       delete TakFlying.oCtrl;
  125.       MiniGameManager.__get__Instance().doRemoveListener(this);
  126.       this.oFlappingSound.doDestroy();
  127.       delete this.oFlappingSound;
  128.    }
  129.    function get FollowY()
  130.    {
  131.       return this.nFollowY;
  132.    }
  133.    function get Ref()
  134.    {
  135.       return this.mcRef;
  136.    }
  137.    function doFlyUp()
  138.    {
  139.       this.doMoveY();
  140.       this.nLeftLimit = TakFlying.UP_LEFT_LIMIT;
  141.       this.nRightLimit = TakFlying.UP_RIGHT_LIMIT;
  142.       this.nSpeedXMaxModifier = -2;
  143.       this.doMoveX();
  144.       this.bFlyingDown = false;
  145.       if(this.nSpeedY >= -2)
  146.       {
  147.          this.setState("FlyChange");
  148.       }
  149.    }
  150.    function doFlyChange()
  151.    {
  152.       this.doMoveY();
  153.       this.doMoveX();
  154.       if(this.isStateComplete())
  155.       {
  156.          if(this.oKeysManager.isKeyDown(40))
  157.          {
  158.             this.setState("FlyDownFast");
  159.          }
  160.          else if(this.oKeysManager.isKeyDown(38))
  161.          {
  162.             this.setState("FlyDownSlow");
  163.          }
  164.          else
  165.          {
  166.             this.setState("FlyDown");
  167.          }
  168.       }
  169.    }
  170.    function doFlyDownSlow()
  171.    {
  172.       this.nSpeedYMaxModifier = -10;
  173.       this.nSpeedY = Library.Utils.MoreMath.getReachNum(this.nSpeedY,TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier,1.5);
  174.       this.doFall();
  175.    }
  176.    function doFlyDownFast()
  177.    {
  178.       this.nSpeedYMaxModifier = 50;
  179.       this.nSpeedY = Library.Utils.MoreMath.getReachNum(this.nSpeedY,TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier,2.5);
  180.       this.doFall();
  181.    }
  182.    function doFlyDown()
  183.    {
  184.       this.nSpeedYMaxModifier = 0;
  185.       this.doFall();
  186.    }
  187.    function doFall()
  188.    {
  189.       this.doMoveY();
  190.       this.nLeftLimit = TakFlying.DOWN_LEFT_LIMIT;
  191.       this.nRightLimit = TakFlying.DOWN_RIGHT_LIMIT;
  192.       this.nSpeedXMaxModifier = 0;
  193.       this.doMoveX();
  194.       this.bFlyingDown = true;
  195.    }
  196.    function doMoveY()
  197.    {
  198.       if(this.nSpeedY < 0)
  199.       {
  200.          this.nSpeedY += TakFlying.SPEED_LOOSE;
  201.       }
  202.       else
  203.       {
  204.          this.nSpeedY += TakFlying.GRAVITY_GAIN;
  205.          if(this.nFollowY <= -230)
  206.          {
  207.             this.nFollowY = -230;
  208.          }
  209.          else
  210.          {
  211.             this.nFollowY -= 5;
  212.          }
  213.       }
  214.       if(this.nSpeedY > TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier)
  215.       {
  216.          this.nSpeedY = Library.Utils.MoreMath.getReachNum(this.nSpeedY,TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier,0.5);
  217.       }
  218.       if(this.mcRef._y + this.nSpeedY < 0)
  219.       {
  220.          this.mcRef._y += this.nSpeedY;
  221.       }
  222.       else
  223.       {
  224.          this.setState("FlyDown");
  225.          this.doLockState();
  226.          this.mcRef._y += this.nSpeedY;
  227.          MiniGameManager.__get__Instance().doMinigameEnd();
  228.       }
  229.    }
  230.    function doMoveX()
  231.    {
  232.       if(this.bGoingRight)
  233.       {
  234.          if(this.nSpeedX < 0)
  235.          {
  236.             this.nSpeedX *= TakFlying.DIRECTION_CHANGE_FACTOR_AIR;
  237.          }
  238.          if(this.nSpeedX < TakFlying.MAX_SPEED_AIR + this.nSpeedXMaxModifier)
  239.          {
  240.             this.nSpeedX += TakFlying.ACCELERATION_AIR;
  241.          }
  242.       }
  243.       else if(this.bGoingLeft)
  244.       {
  245.          if(this.nSpeedX > 0)
  246.          {
  247.             this.nSpeedX *= TakFlying.DIRECTION_CHANGE_FACTOR_AIR;
  248.          }
  249.          if(this.nSpeedX > - (TakFlying.MAX_SPEED_AIR + this.nSpeedXMaxModifier))
  250.          {
  251.             this.nSpeedX -= TakFlying.ACCELERATION_AIR;
  252.          }
  253.       }
  254.       else
  255.       {
  256.          this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,TakFlying.ACCELERATION_AIR);
  257.       }
  258.       if(this.mcRef._x + this.nSpeedX > this.nLeftLimit && this.mcRef._x + this.nSpeedX < this.nRightLimit)
  259.       {
  260.          this.mcRef._x += this.nSpeedX;
  261.       }
  262.       else
  263.       {
  264.          this.nSpeedX = 0;
  265.       }
  266.    }
  267.    function doLoadStateAction()
  268.    {
  269.       super.doLoadStateAction();
  270.       switch(this.sState)
  271.       {
  272.          case "FlyChange":
  273.             MiniGameManager.__get__Instance().doStopWinds();
  274.             break;
  275.          case "FlyDown":
  276.             MiniGameManager.__get__Instance().doStartWindNormal();
  277.             if(this.oFlappingSound != undefined)
  278.             {
  279.                this.oFlappingSound.setFadeRate(15);
  280.                this.oFlappingSound.doFadeTo(0);
  281.             }
  282.             break;
  283.          case "FlyDownFast":
  284.             MiniGameManager.__get__Instance().doStartWindFast();
  285.             if(this.oFlappingSound != undefined)
  286.             {
  287.                this.oFlappingSound.setFadeRate(15);
  288.                this.oFlappingSound.doFadeTo(0);
  289.             }
  290.             break;
  291.          case "FlyDownSlow":
  292.             MiniGameManager.__get__Instance().doStartWindSlow();
  293.             if(this.oFlappingSound == undefined)
  294.             {
  295.                this.oFlappingSound = Library.Sound.SoundManager.doPlaySoundInCat(Main.SOUND_CAT_SOUND,"SkySlow.wav",10,999);
  296.                this.oFlappingSound.doAddListener(this);
  297.             }
  298.             this.oFlappingSound.doFadeTo(60);
  299.       }
  300.    }
  301. }
  302.